﻿using Pay.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Pay.DTO;
using Pay.TO;
using Pay.Token.Utils;
using System.Threading.Tasks;

namespace Pay.Token.Controllers {
    /// <summary>
    /// 商品管理
    /// </summary>
    [AppAuthorize("Member")]
    public class ProductManageController : GeneralController {
        private IProductService _iProductService;
        private IMerchantService _iMerchantService;
        private IDistributionProductService _iDistributionProductService;
        private IShopInfoService _iShopInfoService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="iProductService"></param>
        /// <param name="iMerchantService"></param>
        /// <param name="iDistributionProductService"></param>
        public ProductManageController(IProductService iProductService, IMerchantService iMerchantService,
                                       IDistributionProductService iDistributionProductService, IShopInfoService iShopInfoService) {
            _iProductService = iProductService;
            _iMerchantService = iMerchantService;
            _iDistributionProductService = iDistributionProductService;
            _iShopInfoService = iShopInfoService;
        }

        #region 商品列表
        [HttpGet]
        public ActionResult ProductList() {
            var shop_info = _iShopInfoService.GetShopById(UserId);
            ViewBag.ShopInfo = shop_info;
            return View();
        }

        [HttpPost]
        public PartialViewResult _ProductList(TokenProductQuery query) {
            query.merchant_id = UserId;
            var list = _iProductService.GetTokenProductManageList(query);
            ViewBag.img_url = RequestMethod.token_imgurl;
            var curMerchant = _iMerchantService.Get(UserId);
            ViewBag.UserType = (UserType)curMerchant.usertype;
            ViewBag.Status = query.status;
            return PartialView(list);
        }

        /// <summary>
        /// 变更拓垦商城-商品管理状态
        /// </summary>
        /// <param name="product_id"></param>
        /// <param name="distribution_product_id"></param>
        /// <param name="is_proprietary"></param>
        /// <param name="status">状态（1：上架，2：下架，3：删除）</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UpdateProductStatus(long product_id, long distribution_product_id, int is_proprietary, int status) {
            var result = _iProductService.UpdateTokenProductStatus(product_id, distribution_product_id, is_proprietary==1 ? true: false, status, UserId);
            return Json(ResultToJson.Json(result ? " 操作成功" : "操作失败", result), JsonRequestBehavior.AllowGet);
        }
        #endregion
        /// <summary>
        /// 商品分销库
        /// </summary>
        /// <returns></returns>
        public ActionResult Distribution() {
            return View();
        }
        /// <summary>
        /// 商品分销库
        /// </summary>
        /// <returns></returns>
        public PartialViewResult _Distribution(GoodsQuery query) {
            GoodsQuery good_query = new GoodsQuery();
            good_query.page = query.page;
            good_query.size = query.size;
            good_query.sale_status = 0;
            good_query.sell_out = false;
            good_query.IsTokenMall = true;
            good_query.keyword = query.keyword;
            good_query.sort = query.sort;
            var merchant = _iMerchantService.Get(t_RequestMethod.merchant_no);
            good_query.merchant_id = merchant.id;
            var data = _iDistributionProductService.Get(UserId);
            var list = _iProductService.GetForActicity(good_query);
            var curMerchant = _iMerchantService.Get(UserId);
            ViewBag.UserType = (UserType)curMerchant.usertype;
            list.rows.ForEach(c => {
                c.is_distribution = data.Exists(s => s.productid ==c.id && s.merchantid == UserId && s.status == EDistributionProductStatus.InSales);
            });
            ViewBag.img_url = RequestMethod.token_imgurl;
            return PartialView(list);
        }
        /// <summary>
        /// 添加分销
        /// </summary>
        /// <param name="product_id"></param>
        /// <returns></returns>
        public JsonResult SaveShareProduct(long product_id) {
            var model = new distribution_product_dto {
                productid = product_id,
                merchantid = UserId
            };
            int result = _iDistributionProductService.Add(model);
            return Json(ResultToJson.Json(result > 0 ? " 添加成功" : "添加失败", result), JsonRequestBehavior.AllowGet);
        }
    }
}